#!/usr/bin/perl

#===============================================================================
#     REVISION:  $Id$
#  DESCRIPTION:  Test for Meter::Config
#===============================================================================

use strict;
use warnings;

use Readonly;
Readonly our $VERSION => qw($Revision$) [1];

use English qw( -no_match_vars );

#use Smart::Comments;

use Test::More tests => 5;
use Test::Exception;

use Meter::Config;

Readonly my $GOOD_CONFIG =>
    "$ENV{'METER_ROOT'}/tests/meter-www-common-case/meter.conf";

Readonly my $CONFIG_WITH_MISSING_PARAM =>
    "$ENV{'METER_ROOT'}/tests/meter_config/contact_email_param_is_missing.conf";

Readonly my $CONFIG_NON_HASH =>
    "$ENV{'METER_ROOT'}/tests/meter_config/non_hash.conf";

Readonly my $CONFIG_PARSE_ERROR =>
    "$ENV{'METER_ROOT'}/tests/meter_config/parse_error.conf";

Readonly my $NO_SUCH_CONFIG => '/no/such/meter.conf';

sub run_tests {
    lives_ok { get_config($GOOD_CONFIG) } "Load good config: '$GOOD_CONFIG'";

    throws_ok { get_config($CONFIG_WITH_MISSING_PARAM) }
    qr/\QConfig has no 'contact_email' parameter\E/xms,
        "Load config with missing param: '$CONFIG_WITH_MISSING_PARAM'";

    throws_ok { get_config($CONFIG_NON_HASH) }
    qr/\QBad file format: $CONFIG_NON_HASH\E/xms,
        "Try to load non-hash like config: '$CONFIG_NON_HASH'";

    throws_ok { get_config($CONFIG_PARSE_ERROR) }
    qr/\QCannot parse $CONFIG_PARSE_ERROR:\E/xms,
        "Try to load syntax invalid config: '$CONFIG_PARSE_ERROR'";

    throws_ok { get_config($NO_SUCH_CONFIG) }
    qr/\QCannot read $NO_SUCH_CONFIG\E/xms,
        "Try to load non-existent config: '$NO_SUCH_CONFIG'";

    return;
}

run_tests();
